package com.xxl.job.executor.process;

import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.SpringContextUtil;
import com.xxl.job.executor.model.DevInfosModel;
import com.xxl.job.executor.model.GatherDefModel;
import com.xxl.job.executor.model.SignalDefDhModel;
import com.xxl.job.executor.service.DhService;
import com.xxl.job.executor.service.GatherDefService;
import com.xxl.job.executor.utils.HttpRequestUtil;
import com.xxl.job.executor.utils.JSONUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件描述
 *
 * @author wang.kunlin
 * @date 2020年09月28日 9:49
 */
public class DhProcess {

    private String param;

    public DhProcess(String param) {
        this.param = param;
    }

    public void collectProcessor() throws Exception {
        for (int i = 0; i < 50000; i++) {

            XxlJobLogger.log("*************************************第"+i+"次******************************");


            // SpringContextUtil.getBean(GatherDefService.class)   相当于  Autowise  GatherDefService
            //
            List<GatherDefModel> gatherDefModelList = SpringContextUtil.getBean(GatherDefService.class).findGatherDefModelList(param);


            if (gatherDefModelList == null || gatherDefModelList.size() <= 0) {
                XxlJobLogger.log("未获取到采集口[" + param + "]的配置信息！采集退出...");
                return;
            }
            try {
                for (int num = 0; num < gatherDefModelList.size(); num++) {
                    String ip = gatherDefModelList.get(num).getCollectIp(); //获取IP
                    String gatherResId = gatherDefModelList.get(num).getResId();
                    int port = Integer.parseInt(gatherDefModelList.get(num).getCollectPort()); //获取端口
                    String remark = gatherDefModelList.get(num).getRemark();


                    String url = "http://" + ip + ":" + port + "/api/broadcast/querystatus";//接口地址

                    String params = "{\"method\": \"query\"," +
                            "\"phoneList\": ["+remark+"]" +
                            "}";
                    String s = null;//调用HTTP get方法
                  //  XxlJobLogger.log("............开始................");
                    s = HttpRequestUtil.send(url, JSONObject.parseObject(params), "UTF-8");

                 //   XxlJobLogger.log(s);

                    Map<String, Object> map = new HashMap<String, Object>();
                    //返回的字符串封装成map
                    map = JSONUtils.json2map(s);
                    int flag = 0;
                    ArrayList<DevInfosModel> dataDevModelArrayList= new ArrayList<DevInfosModel>();
                    int a = 0;
                    //key和value都循环
                    for(Map.Entry<String,Object> entry : map.entrySet()){
                       // System.out.println("key="+entry.getKey()+" value="+entry.getValue());
                        String value = (String) entry.getValue();
                        if(value.equals("idle")){
                            value = "0";//空闲
                        }else if(value.equals("ring")){
                            value = "1";//振铃
                        }else if(value.equals("busy")){
                            value = "2";//通话中

                        }else {
                            value = "4";//未注册
                        }
                        SignalDefDhModel automaticControlModel = new SignalDefDhModel();
                        automaticControlModel.setGatherId(param);
                        automaticControlModel.setGatherResId(gatherResId);
                        automaticControlModel.setPointAddr(entry.getKey());

                        a = a+1;
                        if(a== map.entrySet().size()){
                            flag = 1;
                        }
                        if(value.equals("4")){
                            automaticControlModel.setSignalType("NEN");//设备状态信号
                            automaticControlModel.setSignalValue("1");
                            SpringContextUtil.getBean(DhService.class).save(automaticControlModel, flag,dataDevModelArrayList);

                        }else{
                            automaticControlModel.setSignalType("NEN");//设备状态信号
                            automaticControlModel.setSignalValue("0");
                            SpringContextUtil.getBean(DhService.class).save(automaticControlModel, 0,dataDevModelArrayList);
                            automaticControlModel.setSignalType("RPH");//通讯状态信号
                            automaticControlModel.setSignalValue(value);
                            SpringContextUtil.getBean(DhService.class).save(automaticControlModel, flag,dataDevModelArrayList);

                        }

                    }
                    //XxlJobLogger.log("............结束................");


                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                if (e instanceof InterruptedException) {
                    throw e;
                }
                e.printStackTrace();
            }
        }
    }
}